package com.qf.web;

import com.alibaba.fastjson.JSON;
import com.qf.web.common.R;
import com.qf.web.pojo.Student;
import com.qf.web.service.StudentService;
import com.qf.web.service.impl.StudentServiceImpl;
import com.qf.web.utils.DateUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

@WebServlet("/addStudent")
public class AddStudentServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");


        String sex = req.getParameter("sex");

        String birthday = req.getParameter("birthday");

        String phone = req.getParameter("phone");

        String address = req.getParameter("address");

        String hobby = req.getParameter("hobby");

        Student student = new Student();
        student.setName(name);



        Date date = DateUtil.strToDate(birthday, "yyyy-MM-dd");
        student.setBirthday(date);

        student.setPhone(phone);

        student.setAddress(address);

        student.setHobby(hobby);

        student.setSex(sex);

        StudentService studentService = new StudentServiceImpl();

        int result = studentService.AddStudent(student);

        if (result == 1) {
            String s = JSON.toJSONString(R.ok());
            resp.getWriter().println(s);
        } else {
            String s = JSON.toJSONString(R.error());
            resp.getWriter().println(s);
        }
    }
}
